<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
use App\Model\Article;
use Session;
use Cache;
class IndexController extends Controller
{
    public function test1()
    {
        return phpinfo();
    }
    public function test2(Request $request){
       $data=$request->all();
       dump($data);

       if($request->has('name')){
           $name=$request->get('name');
           dd($name);
       }

    }
    public function  add(Request $request){

//       $data=[
//            'name'=>'bowen',
//             'password'=>md5('4655182')
//       ];
//       $res=DB::table('admin')->insert($data);
//       dd($res);
//        $article=new Article();
//        $article->title='dege';
//        $article->keywords='ddd';
//        $article->desc='ddd';
//        $article->author='ddd';
//        $article->thumb='ddd';
//        $article->content='ddd';
//        $article->click=11;
//        $article->zan=11;
//        $article->rec=11;
//        $article->time=time();
//        $article->cateid=25;
//        $res=$article->save();
//        dd($res);
    }
    public function select(){
        $db=DB::table('admin');
        $data=$db->orderBy('id','desc')->limit(1)->offset(1)->get();
        foreach($data as $key => $value)
        {
            echo "id是{$value->id},名字是:{$value->name},密码是{$value->password}<br/>";
        }
        echo '<br/>';
        $data30=$db->where('id','30')->get();
        dd($data30);
    }
    public  function edit(){
        $res=DB::table('admin')->where('id',30)->update(['name'=>'dege']);
        dd($res);
    }
    public function del(){
        $db=DB::table('admin');
        $db->where('id','30')->delete();
        $data=$db->get();
        foreach($data as $key=>$value)
        {
            echo 'id是{$value->id},name是{$value->name} <br/>';
        }

    }
    public function join(){
//        $sql ="select c.catename as catename,a.* from bk_article a LEFT JOIN bk_cate c on a.cateid=c.id";
//        $data=DB::select($sql);
        $article=new Article();
         $data=$article->paginate(3);
         //$dege=date('Y-m-d',time());
        return view('test/dege',['data'=>$data]);
    }
    public function test6(){

        return view('test/zfb');
    }
    public function test7(Request $request){
        $article=new Article();
        $this->validate($request,['title'=>'required|unique:article',
            'desc'=>'required',
            'zan'=>'required|integer',
            'yzm'=>'required|captcha'
        ]);
        if($request->hasFile('thumb')&&$request->file('thumb')->isValid())
        {
            $name=sha1(time().rand(100000,999999)).'.'.$request->file('thumb')->extension();
            $request->file('thumb')->move('./static/upload',$name);
            $path='/static/upload/'.$name;
        }

        $data=$request->except(['_token','thumb','yzm']);
        $data['thumb']=isset($path)?$path:'';

        $res=$article->insertGetId($data);
        dd($res);

    }
    public function test8(){
        $data=Article::all();
        Cache::add('dege','15cm',10);
        $rsp=Cache::get('dege');
        $value=Cache::get("article_title",function(){
            return Article::get();
        });
        return $value;
    }
    public function getAccessToken(){
        return Cache::remember('access_token',100,function(){

            $api='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config('commom.appID').'&secret='.config('commom.appsecret');
            $data=$this->http_get($api);
            $tmpArr=json_decode($data,true);
            dd($tmpArr);
//            $accessToken=$tmpArr['access_token'];
//            return $accessToken;
        });
    }
    public  function http_get($url){//geturl函数

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($ch);

    curl_close($ch);

    return $output;

    }


}
